package com.hsp.struct.queue;

import java.util.Scanner;

/**
 * @version 1.0
 * @Author: 许啸林
 * @Date: 2021/9/27 15:29
 * 使用数组实现单向队列
 * 存在问题: 数组不能重复使用
 */
public class ArrayQueue {
    /**
     *  队列容量
     */
     private int maxSize;
     /**
      * 队列头部指针
      * */
     private int front;
     /**
     * 队列尾部指针
     * */
     private int rear;
    /**
     * 队列存放数组
     */
     private int[] arr;

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show) 显示队列");
            System.out.println("a(add)  加入队列");
            System.out.println("g(get)  取出队列");
            System.out.println("h(head) 查看队列头");
            System.out.println("e(head) 退出队列");
            char c = scanner.next().charAt(0);
            switch (c) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int i = scanner.nextInt();
                    arrayQueue.add(i);
                    break;
                case 'g':
                    try {
                        int queueNum = arrayQueue.getQueue();
                        System.out.println("取出队列值:" + queueNum);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int headNum = arrayQueue.headQueue();
                        System.out.println(headNum);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    System.out.println("退出");
                    loop = false;
                    break;
                default:
                    break;
            }
        }

    }

    /**
     * 创建队列构造器
     * @param maxSize
     */
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        // front 指向队列头的前一个位置
        this.front = -1;
        // rear 指向队列尾(即队列的最后一个数据)
        this.rear  =-1;
    }

    /**
     * 判断队列是否存满
     * @return
     */
    public boolean isFull() {
        return  maxSize -1 == rear;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty() {
        return  rear == front;
    }

    /**
     *　向队列中加入数据
     * @return
     */
    public void add(int n) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        rear++;
        arr[rear] = n;
        return;
    }

    /**
     * 从队列中取出数据
     * @return
     */
    public int getQueue() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        front ++;
        return arr[front];
    }

    /**
     * 显示队列所有数据
     */
    public void showQueue() {
        if(isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for(int i = 0; i < maxSize; i++) {
            System.out.printf("arr[%d] = [%d]\n",i,arr[i]);
        }
    }

    /**
     *
     * 显示队列头数据
     * @return
     */
    public int headQueue() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return  arr[front + 1];
    }
}


